package com.tarena.luban.worker.server.repo;

import com.tarena.luban.worker.server.bo.WorkerBO;
import com.tarena.luban.worker.server.dao.WorkerDao;
import com.tarena.luban.worker.server.repo.converter.WorkerConverter;
import com.tarena.luban.worker.server.repository.WorkerRepository;
import com.tarena.tp.luban.worker.po.Worker;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/**
 * @author java@tedu.cn
 * @version 1.0
 */
@Repository
public class WorkerRepoImpl implements WorkerRepository {
    @Autowired
    private WorkerDao workerDao;
    @Autowired
    private WorkerConverter workerConverter;
    @Override
    public void delete(Long userId) {
        //delete from worker where user_id=#{userId}
        workerDao.deleteByUserId(userId);
    }

    /**
     * 将参数进行转化
     * 写入数据库使用的对象 Worker
     * @param workerCreateParam
     * @return
     */
    @Override
    public Long save(WorkerCreateParam workerCreateParam) {

        //转化成po
        Worker worker = workerConverter.param2po(workerCreateParam);
        //UNDO 判断worker id是否不是空 不是空save方法执行实际更新
        workerDao.insert(worker);
        //利用mybatis的自增,自填补功能
        return worker.getId();
    }

    @Override
    public WorkerBO getWorker(Long userId) {
        //TODO redis缓存 key="workder:detail:"+userId
        //持久层 PO
        Worker worker = workerDao.selectByUserId(userId);
        //转化返回
        return workerConverter.po2bo(worker);
    }
}
